﻿using System;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace VarianceReportWinUI
{
    public class UIHandler : IDisposable
    {
        public DockPanel dockPanel = null;
        public frmWorkQueue queuePanel = new frmWorkQueue();
        public WorkQueue workQueue;

        public UIHandler(DockPanel pdockPanel)
        {
            this.workQueue = WorkQueue.Load();
            this.dockPanel = pdockPanel;
            this.InitPanels();
            this.queuePanel.UpdateViewer();

        }

        private void InitPanels()
        {
            this.queuePanel.MdiParent = this.ParentForm;
            this.queuePanel.Show(this.dockPanel);
            this.queuePanel.uiHandler = this;
            this.queuePanel.DockState = DockState.DockLeft;
            this.queuePanel.VisibleState = DockState.DockLeft;
        }

        public void ShowOrHideQueue()
        {
            var mainform = (frmMain)ParentForm;

            if (!this.queuePanel.Visible)
            {
                this.queuePanel.Show();
                mainform.ShowHideWorkQueueButton.Text = "Hide Work Queue";
            }
            else
            {
                this.queuePanel.Hide();
                mainform.ShowHideWorkQueueButton.Text = "Show Work Queue";
            }
        }

        void IDisposable.Dispose()
        {
            this.queuePanel.Close();
            this.queuePanel.Dispose();
            this.queuePanel = null;
            this.dockPanel.Dispose();
            this.dockPanel = null;
        }


        public void ViewWorkItem(WorkItem pWorkItem)
        {
            foreach (DockPane pane in this.dockPanel.Panes)
            {
                foreach (DockContentEx content in pane.Contents)
                {
                    if ((content is frmWorkItem) && (((frmWorkItem)content).FormKey == pWorkItem.Key))
                    {
                        content.BringToFront();
                        content.UpdateViewer();

                        return;
                    }
                }
            }

            frmWorkItem tFrmWorkItem = new frmWorkItem();
            tFrmWorkItem.MdiParent = this.ParentForm;
            tFrmWorkItem.Show(this.dockPanel);
            tFrmWorkItem.DockState = DockState.Document;
            tFrmWorkItem.VisibleState = DockState.Document;
            tFrmWorkItem.uiHandler = this;
            tFrmWorkItem.FormKey = pWorkItem.Key;

            tFrmWorkItem.View(pWorkItem);
        }

        public void CloseWorkItem(WorkItem pWorkItem)
        {
            foreach (DockPane pane in this.dockPanel.Panes)
            {
                foreach (DockContentEx content in pane.Contents)
                {
                    if ((content is frmWorkItem) && (((frmWorkItem)content).FormKey == pWorkItem.Key))
                    {
                        ((frmWorkItem)content).Close();

                        return;
                    }
                }
            }


        }

        public Form ParentForm
        {
            get
            {
                return (Form)this.dockPanel.Parent;
            }
        }

        public void NewWorkItem()
        {
            queuePanel.NewWorkItem();
        }

    }
}
